package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.GongNengDao;
import net.mysoft.budgetsoft.system.dao.GongNengJSDao;
import net.mysoft.budgetsoft.system.dao.JueSeDao;
import net.mysoft.budgetsoft.system.domain.TGongNeng;
import net.mysoft.budgetsoft.system.domain.TGongNengJS;
import net.mysoft.budgetsoft.system.domain.TJueSe;
import net.mysoft.budgetsoft.system.pagemodel.DictionaryDetail;
import net.mysoft.budgetsoft.system.pagemodel.JueSe;
import net.mysoft.budgetsoft.system.service.JueSeService;
import net.mysoft.framework.cache.Cache;
import net.mysoft.framework.cache.CacheManager;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2013-12-27 14:09:49
 */
@Service("jueSeService")
public class JueSeServiceImpl implements JueSeService {
	@Resource
	private JueSeDao jueSeDao;
	@Resource
	private GongNengJSDao gongNengJSDao;
	@Resource
	private GongNengDao gongNengDao;

	public JueSeDao getJueSeDao() {
		return this.jueSeDao;
	}

	public void setJueSeDao(JueSeDao jueSeDao) {
		this.jueSeDao = jueSeDao;
	}


	public GongNengJSDao getGongNengJSDao() {
		return gongNengJSDao;
	}

	public void setGongNengJSDao(GongNengJSDao gongNengJSDao) {
		this.gongNengJSDao = gongNengJSDao;
	}

	public GongNengDao getGongNengDao() {
		return gongNengDao;
	}

	public void setGongNengDao(GongNengDao gongNengDao) {
		this.gongNengDao = gongNengDao;
	}
	private String addWhere(JueSe jueSe, String hql, List<Object> params) {
		if (jueSe.getJueSeMC() != null && !"".equals(jueSe.getJueSeMC())) {
			hql += "and t.jueSeMC like ?";
			params.add("%" + jueSe.getJueSeMC() + "%");
		}
		if(jueSe.getZhuangTai()!=null && !"".equals(jueSe.getZhuangTai())){
			hql += " and t.zhuangTai = ? ";
			params.add(jueSe.getZhuangTai());
		}
		return hql;
	}
	/**
	 * 角色权限设置
	 */
	private void saveAuthotity(JueSe jueSe, TJueSe tJueSe) {
		List list = gongNengJSDao.find("from TGongNengJS t where t.jueSeBH = ? ",new Object[]{tJueSe.getBianHao()});
		gongNengJSDao.delete(list);
		//SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		if (jueSe.getGongNengBH() != null && !"".equals(jueSe.getGongNengBH())) {
			String[] gongNengBHArr = jueSe.getGongNengBH().split(",");
			for (String gongNengBH : gongNengBHArr) {
				TGongNengJS ta = new TGongNengJS();
				ta.setBianHao(UUID.randomUUID().toString());
				ta.setGongNengBH(gongNengBH.trim());
				ta.setJueSeBH(tJueSe.getBianHao());
				gongNengJSDao.save(ta);
			}
		}
	}

	@Override
	public void add(JueSe jueSe) {
		TJueSe t = new TJueSe();
		BeanUtils.copyProperties(jueSe, t);
		t.setChuangJianSJ(new Date());
		jueSeDao.save(t);
		saveAuthotity(jueSe, t);
	}

	@Override
	public void edit(JueSe jueSe) {
		TJueSe t = new TJueSe();
		BeanUtils.copyProperties(jueSe, t);
		jueSeDao.update(t);
		saveAuthotity(jueSe, t);
	}

	@Override
	public void delete(JueSe jueSe) {
		for (String id : jueSe.getIds().split(",")) {
			Object t = jueSeDao.get(TJueSe.class, id);
			List list = gongNengJSDao.find("from TGongNengJS t where t.jueSeBH = ?", new Object[] { id });
			gongNengJSDao.delete(list);
			if (t != null) {
				jueSeDao.delete(t);
			}
		}
	}

	@Override
	public DataGrid datagrid(JueSe jueSe) {
		DataGrid dg = new DataGrid();
		String hql = "from TJueSe t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(jueSe, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(jueSe, hql);	
		List<TJueSe> toList = jueSeDao.find(hql, params, jueSe.getPage(), jueSe.getRows());
		List<JueSe> oList = new ArrayList<JueSe>();
		oList = changeModel(toList);
		dg.setTotal(jueSeDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}

	@Override
	public List<JueSe> combobox() {
		List<JueSe> rl = new ArrayList<JueSe>();
		List<TJueSe> l = jueSeDao.find("from TJueSe t where t.zhuangTai = 11 ");
		if (l != null && l.size() > 0) {
			for (TJueSe t : l) {
				JueSe r = new JueSe();
				r.setBianHao(t.getBianHao());
				r.setJueSeMC(t.getJueSeMC());
				rl.add(r);
			}
		}
		return rl;
	}


	private String addOrder(JueSe jueSe, String hql) {
		if (jueSe.getSort() != null) {
			hql += " order by " + jueSe.getSort() + " " + jueSe.getOrder();
		}
		return hql;
	}
	public List<JueSe> changeModel(List<TJueSe> tJueSe) {
		List<JueSe> list = new ArrayList<JueSe>();
		Cache cac = CacheManager.getContent("ZhuangTai");
		List<DictionaryDetail> ddList = new ArrayList();
		if(cac!=null){
			ddList = (List<DictionaryDetail>) cac.getValue();
		}
		
		for (TJueSe to : tJueSe) {
			List<TGongNengJS> gongNengJSList = gongNengJSDao.find("from TGongNengJS t where t.jueSeBH = ?", new Object[] { to.getBianHao() });
			StringBuffer sb = new StringBuffer();
			StringBuffer sb2 = new StringBuffer();
			for (TGongNengJS a : gongNengJSList) {
				TGongNeng tf = (TGongNeng) gongNengDao.get("from TGongNeng t where t.bianHao = ? ", new Object[] { a.getGongNengBH() });
				if (tf != null) {
					sb.append(a.getGongNengBH());
					sb.append(",");
					sb2.append(tf.getMingCheng() + ",");
				}
			}
			String fids = "";
			String fnames = "";
			if (sb.length() > 0) {
				fids = sb.substring(0, sb.length() - 1);
			}
			if (sb2.length() > 0) {
				fnames = sb2.substring(0, sb2.length() - 1);
			}
			JueSe o = new JueSe();
			BeanUtils.copyProperties(to, o);
			for(DictionaryDetail dd:ddList){
				if(to.getZhuangTai()!=null){
					if(dd.getDetailID().equals(to.getZhuangTai().toString())){
						o.setZhuangTaiStr(dd.getDetailName()); 
						break;
					}
				}
			}
			o.setGongNengBH(fids);
			o.setGongNengMC(fnames);
			list.add(o);
		}
		return list;
	}

	@Override
	public boolean check(JueSe jueSe) {
		TJueSe tJueSe= new TJueSe();
		tJueSe.setBianHao(jueSe.getBianHao());
		List list = gongNengJSDao.find("from TYongHuJS t where t.tJueSe = ? ", new Object[] {tJueSe});
		if(list!=null && list.size()>0){
			return true;
		}
		return false;
	}

		
}
